//
//  InsuMessageListCell.m
//  carcareIOS
//
//  Created by 王然 on 16/9/2.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageListNormalCell.h"

#define Section_Time_Height (33)
#define Section_Height (9)
#define Title_Label_X (17)
#define Title_Label_W (__MAIN_WIDTH - (CarListV_Line_Left + Title_Label_X) * 2)
#define Title_Label_Y (13)
#define Title_View_Height (Title_Label_Y + 21)
#define Time_Label_height ((Label_Y * 2) + Label_Height)
#define Time_ImageV_Height (Time_Label_height + 158 * __MAIN_RATIO_375)
#define Bottom_View_Height (31)

@interface MessageListNormalCell()

@property (nonatomic , strong) UILabel * sectionTimeL;
@property (nonatomic , strong) UIView * backV;
@property (nonatomic , strong) UIView * cornerV;
@property (nonatomic , strong) UILabel * titleL;
@property (nonatomic , strong) MessageListV * messageV;
@property (nonatomic , strong) UIView * timeAndImageV;
@property (nonatomic , strong) UILabel * dateL;
@property (nonatomic , strong) UIImageView * imageV;
@property (nonatomic , strong) UIView * bottomV;

@end
@implementation MessageListNormalCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [Theme backGroundColor];
        [self createUI];
    }
    return self;
}
- (void)createUI
{
    self.sectionTimeL = [[[UILabel labelWithFrame:CGRectMake(CarListV_Line_Left, 0, __MAIN_WIDTH - CarListV_Line_Left * 2, Section_Time_Height) font:FONT_DEFAULT_Light(12) text:@"" textColor:COLOR_HEXSTRING(@"#b0b0b0")] setupOnView:self] labelResetTextAlignment:NSTextAlignmentCenter];

    self.backV = [[[[[UIView viewWithFrame:CGRectMake(self.sectionTimeL.left, self.sectionTimeL.bottom, self.sectionTimeL.width, 10)] setupOnView:self] resetBackgroundColor:[UIColor whiteColor]] resetBorderWidth:0.5 borderColor:[Theme lineColor]] resetCornerRadius:2];
    self.titleL = [[UILabel labelWithFrame:CGRectMake(Title_Label_X, Title_Label_Y, Title_Label_W, Title_View_Height - Title_Label_Y) font:FONT_DEFAULT_Light(16) text:@"" textColor:Label_Color_List] setupOnView:self.backV];
    
    CGFloat cornerW = 6;
    self.cornerV = [[[[UIView viewWithFrame:CGRectMake(self.titleL.left, self.titleL.bottom - self.titleL.height / 2 - cornerW / 2, cornerW, cornerW)] setupOnView:self.backV] resetBackgroundColor:[Theme orangeColor]] resetCornerRadius:cornerW / 2];
    [self.cornerV setHidden:YES];
    
    self.messageV = [[[MessageListV alloc]initWithWidth:ListV_Width] setupOnView:self];
    [self.messageV setOrigin:CGPointMake(ListV_Left, self.sectionTimeL.bottom + Title_View_Height)];
//    [self.messageV setHidden:YES];
    
    self.timeAndImageV = [[UIView viewWithFrame:CGRectMake(self.titleL.left, self.titleL.bottom, self.titleL.width, Time_ImageV_Height)] setupOnView:self.backV];
    [self.timeAndImageV setHidden:YES];
    
    self.dateL = [[UILabel labelWithFrame:CGRectMake(0, Label_Y, self.timeAndImageV.width, Label_Height ) font:FONT_DEFAULT_Light(13) text:@"" textColor:Label_Date_Color_List] setupOnView:self.timeAndImageV];
    self.imageV = [[UIImageView imageViewWithFrame:CGRectMake(0, self.dateL.bottom + Label_Y, self.timeAndImageV.width, self.timeAndImageV.height - self.dateL.bottom - Label_Y) image:nil] setupOnView:self.timeAndImageV];
    self.imageV.contentMode = UIViewContentModeScaleToFill;
    
    self.bottomV = [[UIView viewWithFrame:CGRectMake(0, self.backV.height - Bottom_View_Height, self.backV.width, Bottom_View_Height)] setupOnView:self.backV];
    UIImage * rightImage = [ImageHelper renderImage:ImageNamed(@"cell_right") withColor:[Theme wordGrayColor]];
    [Factory imageViewWithCenter: CGPointMake(self.titleL.right - rightImage.size.width / 2, self.bottomV.height / 2) image:rightImage onView:self.bottomV];
    [[UILabel labelWithFrame:CGRectMake(self.titleL.left,0, self.titleL.width - rightImage.size.width * 2, self.bottomV.height) font:FONT_DEFAULT_Light(12) text:@"立即查看" textColor:self.titleL.textColor] setupOnView:self.bottomV];
    [[[UIView viewWithFrame:CGRectMake(self.titleL.left, 0, self.titleL.width, 0.5)]resetBackgroundColor:[Theme lineColor] ]setupOnView:self.bottomV];

}
- (void)reloadDataWithModel:(MessageModel *)model type:(MessageCenter_Type)pageType
{
    if(self.clickLabelURL) self.messageV.clickLabelURL = self.clickLabelURL;
    [self.sectionTimeL setHidden:(pageType == MessageCenter_System)];
    [self.backV setTop:(pageType == MessageCenter_System ? Section_Height : self.sectionTimeL.bottom)];
    [self.backV setHeight: Title_View_Height + ((pageType == MessageCenter_Insurance && ([model.IMG_URL isNotNil])) ?   Time_ImageV_Height : [MessageListV getListCellHeightWithModel:model]) + ((pageType == MessageCenter_Insurance && [model.JUMP_URL isNotNil]) ? Bottom_View_Height : 0)];
    
    [self.titleL setTextColor:(((pageType == MessageCenter_System && model.isExpired) || (pageType == MessageCenter_Service && model.STATUS))? Label_Date_Color_List : Label_Color_List)];
    
    [self.bottomV setHidden:!(pageType == MessageCenter_Insurance && [model.JUMP_URL isNotNil])];
    [self.bottomV setTop:self.backV.height - Bottom_View_Height];
    
    [self.messageV setTop: (pageType == MessageCenter_System ? Section_Height : self.sectionTimeL.bottom) + Title_View_Height];
    
    
    [self.timeAndImageV setHidden:!(pageType == MessageCenter_Insurance &&[model.IMG_URL isNotNil])];
    [self.messageV setHidden:!self.timeAndImageV.hidden];
    
    if([model.IMG_URL isNotNil])[self.imageV setImageWithURL:[NSURL URLWithString:model.IMG_URL]];
    if(!self.messageV.hidden)[self.messageV reloadListDataWithModel:model type:pageType];
    
    [self.cornerV setHidden:model.STATUS];
    self.titleL.left = self.cornerV.hidden ? self.timeAndImageV.left : (self.cornerV.right + self.cornerV.width);
    self.titleL.width = self.cornerV.hidden ? self.timeAndImageV.width : (self.timeAndImageV.width - self.cornerV.width * 2);
    
    self.sectionTimeL.text = (pageType != MessageCenter_System ? model.messageTimeDate : @"");
    self.titleL.text = model.TITLE;
    self.dateL.text = model.dateTime;

}
+ (CGFloat)getCellHeightWithModel:(MessageModel *)model type:(MessageCenter_Type)pageType
{

    return (pageType == MessageCenter_System ? Section_Height : Section_Time_Height)+ Title_View_Height + ((pageType == MessageCenter_Insurance && ([model.IMG_URL isNotNil])) ?   Time_ImageV_Height : [MessageListV getListCellHeightWithModel:model]) + ((pageType == MessageCenter_Insurance && [model.JUMP_URL isNotNil]) ? Bottom_View_Height : 0);
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
